<?php

/**
 * crpLicense
 *
 * @copyright (c) 2009, Daniele Conca
 * @link http://code.zikula.org/crplicense Support and documentation
 * @author Daniele Conca <conca.daniele@gmail.com>
 * @license GNU/GPL - v.2.1
 * @package crpLicense
 */

Loader :: includeOnce('modules/crpLicense/pnclass/crpLicense.php');

/**
 * the main administration function
 *
 * @return string HTML output
 */
function crpLicense_admin_main()
{
	// Security check
	if (!SecurityUtil :: checkPermission('crpLicense::License', '::', ACCESS_EDIT))
	{
		return LogUtil :: registerPermissionError();
	}

	// Create output object
	$pnRender= pnRender :: getInstance('crpLicense', false);

	// Return the output that has been generated by this function
	return $pnRender->fetch('crplicense_admin_main.htm');
}

/**
 * add new item
 *
 * @return string HTML output
 */
function crpLicense_admin_new()
{
	// Security check
	if (!SecurityUtil :: checkPermission('crpLicense::License', '::', ACCESS_EDIT))
	{
		return LogUtil :: registerPermissionError();
	}

	$license= new crpLicense();
	return $license->newLicense();
}

/**
 * create a license
 * @param 'name' the title of the license
 * @param 'descr' the content of the license
 * @param 'url' the url for the license
 */
function crpLicense_admin_create()
{
	// Security check
	if (!SecurityUtil :: checkPermission('crpLicense::License', '::', ACCESS_EDIT))
	{
		return LogUtil :: registerPermissionError();
	}

	$license= new crpLicense();
	return $license->createLicense();
}

/**
 * modify a license
 *
 * @param 'id_license' the id of the item to be modified
 * @return string HTML output
 */
function crpLicense_admin_modify($args)
{
	// Security check
	if (!SecurityUtil :: checkPermission('crpLicense::License', '::', ACCESS_EDIT))
	{
		return LogUtil :: registerPermissionError();
	}

	$license= new crpLicense();
	return $license->modifyLicense();
}

/**
 * update license
 *
 * @param 'id_license' the id of the license
 * @param 'name' the title of the license
 * @param 'descr' the content of the license
 * @param 'url' the url of the license
 */
function crpLicense_admin_update()
{
	// Security check
	if (!SecurityUtil :: checkPermission('crpLicense::License', "::", ACCESS_EDIT))
	{
		return LogUtil :: registerPermissionError();
	}

	$license= new crpLicense();
	return $license->updateLicense();
}

/**
 * delete item
 *
 * @param 'id_license' the id of the video
 * @param 'confirmation' confirmation that this item can be deleted
 * @return mixed string HTML output if no confirmation otherwise true
 */
function crpLicense_admin_delete($args)
{
	$id_license= FormUtil :: getPassedValue('id', isset ($args['id']) ? $args['id'] : null, 'REQUEST');
	$objectid= FormUtil :: getPassedValue('objectid', isset ($args['objectid']) ? $args['objectid'] : null, 'REQUEST');
	$confirmation= FormUtil :: getPassedValue('confirmation', null, 'POST');
	if (!empty ($objectid))
	{
		$id_license= $objectid;
	}

	$license= new crpLicense();
	// Get the existing video
	$item= $license->dao->getAdminData($id_license, false);

	if ($item == false)
	{
		return LogUtil :: registerError(_NOSUCHITEM, 404);
	}

	// Security check
	if (!SecurityUtil :: checkPermission('crpLicense::License', "::", ACCESS_DELETE))
	{
		return LogUtil :: registerPermissionError();
	}

	// Check for confirmation.
	if (empty ($confirmation))
	{
		// No confirmation yet
		// Create output object
		$pnRender= pnRender :: getInstance('crpLicense', false);

		// Add a hidden field for the item ID to the output
		$pnRender->assign('id', $id_license);
		$pnRender->assign('item', $item);

		// Return the output that has been generated by this function
		return $pnRender->fetch('crplicense_admin_delete.htm');
	}

	// If we get here it means that the user has confirmed the action

	// Confirm authorisation code
	if (!SecurityUtil :: confirmAuthKey())
	{
		return LogUtil :: registerAuthidError(pnModURL('crpLicense', 'admin', 'view'));
	}

	// Delete the license
	if (pnModAPIFunc('crpLicense', 'admin', 'delete', array (
			'id' => $id_license
		)))
	{
		// Success
		LogUtil :: registerStatus(_DELETESUCCEDED);
	}

	return pnRedirect(pnModURL('crpLicense', 'admin', 'view'));
}

/**
 * view items
 *
 * @param int $startnum the start item id for the pager
 * @return string HTML output
 */
function crpLicense_admin_view()
{
	// Security check
	if (!SecurityUtil :: checkPermission('crpLicense::License', '::', ACCESS_EDIT))
	{
		return LogUtil :: registerPermissionError();
	}

	$license= new crpLicense();
	return $license->adminView();
}

/**
 * modify module configuration
 *
 * @return string HTML output string
 */
function crpLicense_admin_modifyconfig()
{
	// Security check
	if (!SecurityUtil :: checkPermission('crpLicense::License', '::', ACCESS_ADMIN))
	{
		return LogUtil :: registerPermissionError();
	}

	$license= new crpLicense();
	return $license->modifyConfig();
}

/**
 * This is a standard function to update the configuration parameters of the
 * module given the information passed back by the modification form
 */
function crpLicense_admin_updateconfig()
{
	// Security check
	if (!SecurityUtil :: checkPermission('crpLicense::License', '::', ACCESS_ADMIN))
	{
		return LogUtil :: registerPermissionError();
	}

	$license= new crpLicense();
	return $license->updateConfig();
}

/**
 * delete item's file
 *
 * @param int $id_license item identifier
 *
 * @return mixed string HTML output if no confirmation otherwise true
 */
function crpLicense_admin_delete_file($args)
{
	// Security check
	if (!SecurityUtil :: checkPermission('crpLicense::License', '::', ACCESS_DELETE))
	{
		return LogUtil :: registerPermissionError();
	}

	$id_license= FormUtil :: getPassedValue('id', null, 'GET');
	$file_type= FormUtil :: getPassedValue('file_type', null, 'GET');

	$imageObj= new crpLicense();
	if ($imageObj->dao->deleteFile($file_type, $id_license))
		LogUtil :: registerStatus(_DELETESUCCEDED);

	return pnRedirect(pnModURL('crpLicense', 'admin', 'view'));
}

/**
 * modify item status
 *
 * @return string HTML output
 */
function crpLicense_admin_change_status()
{
	// Security check
	if (!SecurityUtil :: checkPermission('crpLicense::License', '::', ACCESS_ADD))
	{
		return LogUtil :: registerPermissionError();
	}

	$license= new crpLicense();
	return $license->changeStatus();
}
?>